package com.team.easybuy.service.Impl;

import com.team.easybuy.entity.Car;
import com.team.easybuy.service.CarService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CarServiceImpl implements CarService {

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public String addProInCar(Car car, Integer id) {
        if (redisTemplate.hasKey("car")) {
            Map<Integer, List<Car>> myCar = (Map<Integer, List<Car>>) redisTemplate.opsForValue().get("car");
            boolean flag = false;
            if (myCar != null) {
                List<Car> carList = myCar.get(id);
                if (carList != null) {
                    for (Car c : carList) {
                        if (c.getpId().equals(car.getpId())) {
                            c.setpCount(c.getpCount() + car.getpCount());
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        carList.add(car);
                    }
                    myCar.put(id, carList);
                    redisTemplate.opsForValue().set("car", myCar);
                } else {
                    List<Car> list = new ArrayList<>();
                    list.add(car);
                    myCar.put(id, list);
                    redisTemplate.opsForValue().set("car", myCar);
                }
            }
            return "再次添加";
        } else {
            Map<Integer, List<Car>> map = new HashMap<>();
            List<Car> carList = new ArrayList<>();
            carList.add(car);
            map.put(id, carList);
            redisTemplate.opsForValue().set("car", map);
            return "第一次添加";
        }
    }

    @Override
    public List<Car> loadCarList(Integer id) {
        if (redisTemplate.hasKey("car")) {
            Map<Integer, List<Car>> myCar = (Map<Integer, List<Car>>) redisTemplate.opsForValue().get("car");
            if (myCar != null) {
                return myCar.get(id);
            }
        }
        return null;
    }

    @Override
    public String addNumByPro(Integer pId, Integer id, Integer stock) {
        if (redisTemplate.hasKey("car")) {
            Map<Integer, List<Car>> myCar = (Map<Integer, List<Car>>) redisTemplate.opsForValue().get("car");
            if (myCar != null) {
                List<Car> carList = myCar.get(id);
                if (carList != null) {
                    for (Car car : carList) {
                        if (car.getpId().equals(pId)) {
                            if (car.getpCount() < stock) {
                                car.setpCount(car.getpCount() + 1);
                            } else {
                                return "不能超过库存数";
                            }
                        }
                    }
                }
                myCar.put(id, carList);
                redisTemplate.opsForValue().set("car", myCar);
                return "OK";
            }
        }
        return "不能超过库存数";
    }

    @Override
    public String delNumByPro(Integer pId, Integer id) {
        if (redisTemplate.hasKey("car")) {
            Map<Integer, List<Car>> myCar = (Map<Integer, List<Car>>) redisTemplate.opsForValue().get("car");
            if (myCar != null) {
                List<Car> carList = myCar.get(id);
                if (carList != null) {
                    for (Car car : carList) {
                        if (car.getpId().equals(pId)) {
                            if (car.getpCount() > 1) {
                                car.setpCount(car.getpCount() - 1);
                            }
                        }
                    }
                }
                myCar.put(id, carList);
                redisTemplate.opsForValue().set("car", myCar);
                return "OK";
            }
        }
        return "";
    }

    @Override
    public boolean delInCar(Integer pId, Integer id) {
        if (redisTemplate.hasKey("car")) {
            Map<Integer, List<Car>> myCar = (Map<Integer, List<Car>>) redisTemplate.opsForValue().get("car");
            if (myCar != null) {
                List<Car> carList = myCar.get(id);
                carList.removeIf(car -> car.getpId().equals(pId));
                myCar.put(id, carList);
                redisTemplate.opsForValue().set("car", myCar);
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean delAllCar(Integer id) {
        if (redisTemplate.hasKey("car")) {
            Map<Integer, List<Car>> myCar = (Map<Integer, List<Car>>) redisTemplate.opsForValue().get("car");
            if (myCar != null) {
                List<Car> carList = myCar.get(id);
                carList.clear();
                myCar.put(id, carList);
                redisTemplate.opsForValue().set("car", myCar);
                return true;
            }
        }
        return false;
    }
}